package com.woniuxy.woniuhis.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.woniuhis.entity.MedicineCost;
import com.woniuxy.woniuhis.entity.OrderCart;
import com.woniuxy.woniuhis.exception.MedicineServiceException;
import com.woniuxy.woniuhis.service.MedicineCostService;
import com.woniuxy.woniuhis.service.OrderCartService;
import com.woniuxy.woniuhis.util.ResponseInfo;


@Controller
public class OrderCartController {

    @Autowired
    OrderCartService orderCartService;
    
    @Autowired
    MedicineCostService medicineCostService;
    
    /**
     * @param req
     * @return
     * 购物车增加方法
     */
    @RequestMapping("declineCart")
    @ResponseBody
    public ModelAndView declineCartController(HttpServletRequest req) {
    	
    		
	    	String mediId = req.getParameter("mediId");
	    	System.out.println(mediId);
		
    	try {
    		
    		OrderCart orderCart = new OrderCart();
    		orderCart.setMediId(mediId);
    		
    		orderCartService.declineCartService(orderCart);
    
        	
        	ResponseInfo responseInfo = new ResponseInfo();
        	

        	
        	responseInfo.setMsg("查询药物");
        	responseInfo.setState(200);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/cart.html");
        	return mv;
		} catch (MedicineServiceException e) {
			System.out.println("declineCartController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("declineCartController服务方法错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/cart.html");
        	return mv;
		}  catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/cart.html");
        	return mv;
		}
    }
    /**
     * @param req
     * @return
     * 购物车增加方法
     */
    @RequestMapping("incrementCart")
    @ResponseBody
    public ModelAndView incrementCartController(HttpServletRequest req) {
    	
	    	String mediId = req.getParameter("mediId");
	    	System.out.println(mediId);
		
    	try {
    		
    		OrderCart orderCart = new OrderCart();
    		orderCart.setMediId(mediId);
    		
    		orderCartService.incrementCartService(orderCart);
    
        	
        	ResponseInfo responseInfo = new ResponseInfo();
        	

        	
        	responseInfo.setMsg("查询药物");
        	responseInfo.setState(200);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/cart.html");
        	return mv;
		} catch (MedicineServiceException e) {
			System.out.println("incrementCartController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("incrementCartController服务方法错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/cart.html");
        	return mv;
		}  catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/cart.html");
        	return mv;
		}
    }
    
    
    /**
     * @param req
     * @return
     * 购物车初始化
     */
    @RequestMapping("initCart")
    @ResponseBody
    public ResponseInfo initCartController(HttpServletRequest req) {
    	
		
    	try {
    		
    		List<OrderCart> carts = orderCartService.findAllCart();
    
        	
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setData(carts);
        	responseInfo.setMsg("查询药物");
        	responseInfo.setState(200);
        	
        	return responseInfo;
		} catch (MedicineServiceException e) {
			System.out.println("findAllMedicineController错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("findMedicineByName服务方法错误");
        	responseInfo.setState(500);
        	return responseInfo;
		}  catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	return responseInfo;
		}
    }
    
    
    
    
    
    
    
    /**
     * @param req
     * @return
     * 添加购物车条目的controller方法
     */
    @RequestMapping("addCart")
    @ResponseBody
    public ModelAndView addMedicineCostController(HttpServletRequest req) {
    	
    	//找到进料编号
    	String mediId = req.getParameter("mediId");
    	System.out.println(mediId);

    	try {
    		//创建order对象
        	OrderCart orderCart = new OrderCart();
        	//判断前端是否传入空值
        	if (mediId!=null&&mediId!="") {
        		orderCart.setMediId(mediId);
			}else {
				throw new NullPointerException();
			}
        	//根据前端传来的mediId,查到medi对象
        	MedicineCost medicineCost = new MedicineCost();
        	medicineCost.setMediId(mediId);
        	List<MedicineCost> realMediCost = medicineCostService.findMedicineCostById(medicineCost);
    		System.out.println(realMediCost);
        	//给order添加其他属性
        	orderCart.setCartId(UUID.randomUUID().toString());
        	orderCart.setManuName(realMediCost.get(0).getMediManu());
        	orderCart.setMediName(realMediCost.get(0).getMediName());
        	orderCart.setMediCost(realMediCost.get(0).getMediCost());
        	
        	//数量的验证
        		//查看此条目是否存在
        		OrderCart fakeOrderCart = new OrderCart();
        		fakeOrderCart.setMediId(mediId);
        		OrderCart isexist = orderCartService.findOrderByMediId(fakeOrderCart);
        		if(isexist==null) {
        			orderCart.setMediAmount(1);
        		}else {
        			orderCart.setMediAmount(1);
        		}
        	//小计
        		if(isexist==null) {
        			orderCart.setSubtotal(realMediCost.get(0).getMediCost());
        		}else {
					orderCart.setSubtotal(realMediCost.get(0).getMediCost());
				}
        	
        	//将购物车存入
        	if(isexist==null) {
        		orderCartService.addOrderCart(orderCart);
        	}else {
				orderCartService.incrementCartService(orderCart);
			}
        		
    		
        		
        	ResponseInfo responseInfo = new ResponseInfo();	
        		
        	if(isexist==null) {
        		responseInfo.setMsg("添加订单成功");
        	}else {
				responseInfo.setMsg("追加订单成功");
			}
        	responseInfo.setState(200);     	
        	
        	//设置mv

        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info", responseInfo);
        	return mv;
		} catch (MedicineServiceException e) {
			System.out.println("addMedicine错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("addMedicine服务方法错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info", responseInfo);
        	return mv;
		} catch (NullPointerException e) {
			System.out.println("不完整错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("请输入全部的数据");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info", responseInfo);
        	return mv;
		} catch (Exception e) {
			System.out.println("其他错误");
			e.printStackTrace();
			
        	ResponseInfo responseInfo = new ResponseInfo();
        	responseInfo.setMsg("其他错误");
        	responseInfo.setState(500);
        	ModelAndView mv = new ModelAndView();
        	mv.setViewName("views/storage/prepay.html");
        	mv.addObject("info", responseInfo);
        	return mv;
		}

}

    
	
}
